package com.jch.zk;


import org.junit.Before;
import org.junit.Test;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;

public class HiveApi {


    private static String driver="org.apache.hive.jdbc.HiveDriver";
    private static String url="jdbc:hive2://192.168.72.101:10000/188a";
    private static String username="root";
    private static String password="123456";

    Connection connection;
    Statement statement;
    ResultSet rs;

    @Before
    public void init() throws Exception{

        //建立连接
        Class.forName(driver);
        connection = DriverManager.getConnection(url, username, password);
        statement = connection.createStatement();

    }


    @Test
    public void show() throws Exception{

        //查询所有表
        String sql="show tables";
        rs = statement.executeQuery(sql);

        while (rs.next()){
            System.out.println(rs.getString(1));
        }

    }

    //编写hiveAPI求出数据中价格在100000到500000之间的数据
    @Test
    public void test1() throws Exception{

        String sql="select gname,xprice from goods where xprice>=100000 and xprice<=500000";
        rs = statement.executeQuery(sql);

        while (rs.next()){

            String gname = rs.getString(1);
            double xprice = rs.getDouble(2);
            System.out.println(gname+"\t"+xprice);
        }



    }



}
